import * as appConfig from 'app-config.js'

App({
	errorMsg(title){
		wx.showToast({
			title,
			icon:'none'
		})
	},
	getMemInfo(){
    	var promise = new Promise((resolve,reject)=>{
    	  wx.getStorage({
    	    key:'memInfo',
    	    success(res){
    	      resolve(res)
    	    },
    	    fail(error){
    	      app.errorMsg(error.errMsg)
    	    }
    	  })
    	})   
    	return promise 
  	},
    request : function(url, data, method){
      var promise = new Promise((resolve, reject) => {
         //init
         var that = this;
         var postData = data;
         /*
         //自动添加签名字段到postData，makeSign(obj)是一个自定义的生成签名字符串的函数
         postData.signature = that.makeSign(postData);
         */
         //网络请求
         wx.request({
            url:appConfig.apiBase+url,
            data: postData,
            method,
            success: function (res) {
              resolve(res.data)
               if (res.data.code == 0) {
                  resolve( res.data );
               } else {
                  reject( res.data );
               }
            },
            error: function (e) {
               reject('网络出错');
            }
         })
      });
      return promise;
   },


 })

 

function memInfo(url) {
  try {
    var value = wx.getStorageSync('memInfo')
    if (value) {
      return value;
    }else{
      wx.navigateTo({
        url: url,
      })
    }
   
  } catch (e) {
    wx.navigateTo({
      url: url,
    })
  }
}


module.exports = {
  memInfo: memInfo
}

